package com.xqboss.chain.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.chain.domain.chain.UpUpdateChainLog;
import com.xqboss.chain.enums.UpChainStatusEnum;
import com.xqboss.chain.mapper.UpUpdateChainLogMapper;
import com.xqboss.common.core.redis.lock.RedisLock;
import com.xqboss.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 上链日志表 服务实现类
 * </p>
 *
 * @author ct
 * @since 2023-06-26
 */
@Service
public class UpUpdateChainLogService extends ServiceImpl<UpUpdateChainLogMapper, UpUpdateChainLog> {


    @Autowired
    private UpUpdateChainService upUpdateChainService;

    /**
     * 根据交易号获取交易记录
     * @param reqNo
     * @return
     */
    public UpUpdateChainLog getByReqNo(String reqNo) {
        UpUpdateChainLog upChainLog = getOne(new LambdaQueryWrapper<UpUpdateChainLog>()
                .eq(UpUpdateChainLog::getReqNo, reqNo));
        return upChainLog;
    }

    /**
     * 更新交易状态
     * @param id
     * @param status
     * @param notifyInfo
     */
    public void updateStatus(Long id, UpChainStatusEnum status, String notifyInfo) {
        UpUpdateChainLog chainLog = getById(id);
        boolean update = update(new LambdaUpdateWrapper<UpUpdateChainLog>()
                .set(UpUpdateChainLog::getNotify, notifyInfo)
                .set(UpUpdateChainLog::getTxStatus, status)
                .eq(UpUpdateChainLog::getId, id));
        // 更新交易状态
        upUpdateChainService.updateStatus(chainLog.getUpUpdateChainId(), status);
        if(!update){
            throw new ServiceException("操作失败！");
        }
    }
}
